package diploma.kisel.modelas.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

import diploma.kisel.hibernate.util.HibernateUtil;
import diploma.kisel.modelas.domain.User;
import diploma.kisel.modelas.repository.api.UserRepository;

public class UserRepositoryImpl implements UserRepository{
	
	EntityManager em = HibernateUtil.getEm();

	@Override
	public List<User> findAll() {
		List<User> users = em.createNamedQuery("User.findAll",
				User.class).getResultList();
		return users;
	}

	@Override
	public User findById(String login) {
		TypedQuery<User> query= em.createNamedQuery("User.findById", User.class);
		query.setParameter("id", login);
		return query.getSingleResult();
	}

	@Override
	public User save(User user) {
		em.getTransaction().begin();
		em.persist(user);
		em.getTransaction().commit();
		return user;
	}

	@Override
	public User update(User user) {
		em.getTransaction().begin();
		em.merge(user);
		em.getTransaction().commit();
		return user;
	}

	@Override
	public void remove(User user) {
		em.getTransaction().begin();
		em.remove(user);
		em.getTransaction().commit();			
	}

}
